<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\store\model\store;

use app\common\model\store\IndexBanner as IndexBannerModel;
// use app\common\model\store\IndexType as IndexTypeModel;
use app\common\model\UploadFile as UploadFileModel;
use app\store\model\Goods as GoodsModel;
use app\store\model\Category as CategoryModel;

/**
 * 商家地址模型
 * Class Address
 * @package app\store\model
 */
class IndexBanner extends IndexBannerModel
{
    /**
     * 获取列表
     * @param array $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(array $param = [])
    {
      $list = $this->where($this->getFilter($param))
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc','sort', $this->getPk()])
            ->paginate(15);
        foreach($list as $k=>$v ){
            if($v['image_id']){
                $img = UploadFileModel::detail($v['image_id']);
                $list[$k]['image'] = $img;
                $list[$k]['image_url'] = $img['preview_url'];
            }else{
                $list[$k]['image'] = null;
                $list[$k]['image_url'] = '';
            }
            if($v['type_id']){
                $type1 = IndexType::detail($v['type_id']);
                $list[$k]['type_name'] = $type1['name'];
            }else{
                $list[$k]['type_name'] = '';
            }

            if($v['second_type_id']){
                $type2 = IndexType::detail($v['second_type_id']);
                $list[$k]['second_type_name'] = $type2['name'];
            }else{
                $list[$k]['second_type_name'] = '';
            }
            if($v['lock_status'] == 1){
                $list[$k]['lock_status_str'] = '锁定';
            }else{
                $list[$k]['lock_status_str'] = '解锁';
            }
            
        }

       return $list;
    }


    /**
     * 获取全部记录
     * @param array $param
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAll(array $param = [])
    {
        return $this->where($this->getFilter($param))
            ->where('is_delete', '=', 0)
            ->order(['sort', $this->getPk()])
            ->select();
    }

    /**
     * 根据param检索查询条件
     * @param array $param
     * @return array
     */
    private function getFilter(array $param = [])
    {
        // 默认查询条件
        $params = $this->setQueryDefaultValue($param, [
            'type' => '',    // 地址类型(10发货地址 20退货地址)
            'search' => '',   // 收货人姓名/联系电话
            'lock_status'=>'' //锁定状态
        ]);
        // 检索查询条件
        $filter = [];
        $params['type'] > 0 && $filter[] = ['type_id', '=', $params['type']];
        $params['lock_status'] >= 0 && $filter[] = ['lock_status', '=', $params['lock_status']];
        !empty($params['search']) && $filter[] = ['describe|imagename', 'like', "%{$params['search']}%"];
        return $filter;
    }

    /**
     * 添加新记录
     * @param array $data
     * @return false|int
     */
    public function add(array $data)
    {
        if(isset($data['category_id']) && $data['category_id'] != ""){
            $categorylist = CategoryModel::detail(['category_id'=>$data['category_id']]);
            if(!$categorylist){
                $this->error = '商品分类ID不存在';
                return false;
            }
        }
        if(isset($data['goods_id']) && $data['goods_id'] != ""){
            // 获取列表记录
            $GoodsModel = new GoodsModel;
            $goodlist = $GoodsModel->detail((int)$data['goods_id']);
            if(!$goodlist){
                $this->error = '商品ID不存在';
                return false;
            }
        }

        if(!isset($data['image_id']) || $data['image_id'] == "0"){
            $this->error = '请上传图片';
            return false;
        }

        $data['store_id'] = self::$storeId;
        return $this->save($data);
    }

    /**
     * 编辑记录
     * @param array $data
     * @return bool|int
     */
    public function edit(array $data)
    {
        if(isset($data['category_id']) && $data['category_id'] != ""){
            $categorylist = CategoryModel::detail(['category_id'=>$data['category_id']]);
            if(!$categorylist){
                $this->error = '商品分类ID不存在';
                return false;
            }
        }
        if(isset($data['goods_id']) && $data['goods_id'] != ""){
            // 获取列表记录
            $GoodsModel = new GoodsModel;
            $goodlist = $GoodsModel->detail((int)$data['goods_id']);
            if(!$goodlist){
                $this->error = '商品ID不存在';
                return false;
            }
        }
        if(!isset($data['image_id']) || $data['image_id'] == "0"){
            $this->error = '请上传图片';
            return false;
        }
        return $this->save($data);
    }

    /**
     * 删除记录
     * @return bool|int
     */
    public function remove()
    {
        return $this->save(['is_delete' => 1]);
    }
}
